package com.tool.finances.lifestyle;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.os.Bundle;
import android.text.method.DialerKeyListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.tool.finances.lifestyle.data.AccountProvider;
import com.tool.finances.lifestyle.data.AccountType;

public class EditeActivity extends Activity {

	private DatePicker datePicker;
	private EditText categoryText;
	private EditText amountText;
	private EditText commentText;
	private ImageButton okButton;
	private ImageButton cancelButton;
	private ImageButton deleteButton;

	private SimpleDateFormat dateFormat;
	private String dateString;
	private String amountString;
	private String categoryString;
	private String commentString;
	private String id;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_dialog);

		dateFormat = new SimpleDateFormat("yyyy/M/dd");
		// categorySource = new ArrayList<String>();

		dateString = getIntent().getExtras().getString("Date");
		amountString = getIntent().getExtras().getString("Amount");
		categoryString = getIntent().getExtras().getString("Category");
		commentString = getIntent().getExtras().getString("Comment");
		id = getIntent().getExtras().getString("Id");

		datePicker = (DatePicker) findViewById(R.id.DateChangPicker);
		categoryText = (EditText) findViewById(R.id.EditTextCategory);
		amountText = (EditText) findViewById(R.id.AmountText);
		amountText.setKeyListener(new DialerKeyListener());
		commentText = (EditText) findViewById(R.id.CommentText);

		try {
			Date date = dateFormat.parse(dateString);
			datePicker.init(date.getYear() + 1900, date.getMonth(), date
					.getDate(), null);

			categoryText.setText(categoryString);
			amountText.setText(amountString);
			commentText.setText(commentString);

		} catch (ParseException e) {
			e.printStackTrace();
		}

		okButton = (ImageButton) findViewById(R.id.OkButton);
		cancelButton = (ImageButton) findViewById(R.id.CancelButton);
		deleteButton = (ImageButton) findViewById(R.id.DeleteButton);

		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					Float.parseFloat(amountText.getText().toString());
					// Integer.parseInt();
				} catch (Exception e) {
					Log.i("error", "Please input number...");
					new AlertDialog.Builder(EditeActivity.this).setMessage(
							"Please input a number to amount").show();
					return;
				}

				ContentValues values = new ContentValues();
				Date date = new Date();
				date.setYear(datePicker.getYear() - 1900);
				date.setMonth(datePicker.getMonth());
				date.setDate(datePicker.getDayOfMonth());
				dateString = dateFormat.format(date);
				values.put(AccountType.Account.ACCOUNTDATE, dateString);
				values.put(AccountType.Account.ACCOUNTMONEY, amountText
						.getText().toString());
				values.put(AccountType.Account.ACCOUNTTYPE, categoryText
						.getText().toString());
				values.put(AccountType.Account.COMMENT, commentText.getText()
						.toString());

				int count = getContentResolver().update(
						AccountProvider.ACCOUNT_CONTENT_URI, values,
						"_id = " + id, null);
				if (count > 0) {
					Toast.makeText(EditeActivity.this, "Update successfully",
							1000).show();
					finish();
				} else {
					Toast.makeText(EditeActivity.this, "Update failed", 1000)
							.show();
				}
			}

		});

		cancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}

		});

		deleteButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				int count = getContentResolver().delete(
						AccountProvider.ACCOUNT_CONTENT_URI, "_id = " + id,
						null);
				if (count > 0) {
					Toast.makeText(EditeActivity.this, "Deleted successfully.",
							1000).show();
					finish();
				} else {
					Toast.makeText(EditeActivity.this, "Deleted failed", 1000)
							.show();
				}
			}

		});
	}
}
